import request from '@/utils/request.js'

/**
 * 微信小程序登录
 * @returns {Promise} 返回登录结果
 */
export function wxLogin() {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: async (loginRes) => {
        try {
          // 这里调用后端接口，将code传给服务器
          const res = await request.get('/login/wxAuth', {
            code: loginRes.code
          });
		  uni.setStorageSync('customerId', res.loginId); 
		  uni.setStorageSync('token', res.tokenValue); 
          resolve(true)
        } catch (error) {
          reject(false)
        }
      },
      fail: (err) => {
        reject(false)
      }
    })
  })
}